//
//  SYEmoticonKeyBoard.swift
//  YANAWeibo
//
//  Created by mac on 16/5/21.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

// 重用标记
private let SYEmoticonCollectionViewCellIdentifier = "SYEmoticonCollectionViewCellIdentifier"

class SYEmoticonKeyBoard: UIView {
    
    // 表情键盘toolbar
    private lazy var toolBar: SYEmoticonToolBar = {
        let view = SYEmoticonToolBar(frame: CGRectZero)
        return view
    }()
    
    // 表情视图
    private lazy var emoticonCollectionView: UICollectionView = {
        let flowLayout = UICollectionViewFlowLayout()
        let view = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        view.pagingEnabled = true
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        // 注册cell
        view.registerClass(SYEmoticonCollectionViewCell.self, forCellWithReuseIdentifier: SYEmoticonCollectionViewCellIdentifier)
        view.backgroundColor = self.backgroundColor
        // 设置数据源代理对象
        view.dataSource = self
        view.delegate = self
        return view
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        addSubview(toolBar)
        addSubview(emoticonCollectionView)
        toolBar.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(self)
            make.height.equalTo(40)
        }
        emoticonCollectionView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBar.snp_top)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.top.equalTo(self)
        }
        
        toolBar.didSelectedEmoticonButtonClosure = { [weak self] (type: SYEmoticonToolBarButtonType) in
            let indexPath: NSIndexPath
            switch type {
            case .Normal:
                indexPath = NSIndexPath(forItem: 0, inSection: 0)
            case .Emoji:
                indexPath = NSIndexPath(forItem: 0, inSection: 1)
            case .Lxh:
                indexPath = NSIndexPath(forItem: 0, inSection: 2)
            }
            // 滚到指定位置
            self?.emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        // 获取布局方式
        let flowout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        flowout.itemSize = emoticonCollectionView.size
        flowout.minimumInteritemSpacing = 0
        flowout.minimumLineSpacing = 0
        // 设置滚动方向
        flowout.scrollDirection = .Horizontal
        
    }
}

extension SYEmoticonKeyBoard: UICollectionViewDataSource, UICollectionViewDelegate {
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return SYEmoticonTools.shareEmoticonTools.allEmoticons.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return SYEmoticonTools.shareEmoticonTools.allEmoticons[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(SYEmoticonCollectionViewCellIdentifier, forIndexPath: indexPath) as! SYEmoticonCollectionViewCell
        cell.emtions = SYEmoticonTools.shareEmoticonTools.allEmoticons[indexPath.section][indexPath.item]
        
        return cell
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let contentOffSetX = scrollView.contentOffset.x
        let cells = emoticonCollectionView.visibleCells().sort { (firstCell, secondCell) -> Bool in
            return firstCell.x < secondCell.x
        }
        // 判断哪个cell显示多
        if cells.count == 2 {
            let firstCell = cells.first!
            let secongCell = cells.last!
            
            let firstCellContentOffSetX = abs(firstCell.x - contentOffSetX)
            let secondCellContentOffSetX = abs(secongCell.x - contentOffSetX)
            
            let indexPath: NSIndexPath
            if firstCellContentOffSetX < secondCellContentOffSetX {
                indexPath = emoticonCollectionView.indexPathForCell(firstCell)!
            } else {
                indexPath = emoticonCollectionView.indexPathForCell(secongCell)!
            }
            
            let section = indexPath.section
            // 设置按钮选中
            toolBar.selectedEmticonButtonWithSection(section)
        }
    }
}




